﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows;

namespace BGM.Application.View
{
    class Pie
    {
        int start = 0;
        double radius = 100;

        public double Radius
        {
            get { return radius; }
            set { radius = value; }
        }
        public void StartPie()
        {
            start = 0;
        }
        public Path GetPiePice(double procentage,string name)
        {
            int piceDeg = (int)(360.00 * (procentage / 100));


            Path p1 = new Path();
            SetCirclePice(p1, piceDeg, start);
            start += piceDeg;
            p1.Fill = new SolidColorBrush(Color.FromRgb((byte)(start + 50), 120, (byte)(start + 50)));
            p1.ToolTip = name;
            return p1;

        }

        private void SetCirclePice(Path fig, int deg, int offset)
        {
            PathFigure myPathFigure = new PathFigure();
            myPathFigure.StartPoint = new Point(radius, radius);

            PathSegmentCollection bb = new PathSegmentCollection();
            PolyQuadraticBezierSegment v = new PolyQuadraticBezierSegment();
            double x = 0, y = 0;
            int degOffset = offset;
            int degSpan = deg;
            Point textPoint= new Point();
            bool textPointIsFound = false;
            for (double i = DegreeToRadian(degOffset); i < DegreeToRadian(degOffset) + DegreeToRadian(degSpan); i += Math.PI / 2000)
            {
                x = (radius * Math.Cos(i));
                y = (radius * Math.Sin(i));

                v.Points.Add(new Point(radius + x, radius + y));
                if (i > DegreeToRadian(degOffset) + DegreeToRadian(degSpan) - DegreeToRadian(degSpan)/2.00 && !textPointIsFound)
                {
                    textPoint = new Point(radius + ((radius / 2.00) * Math.Cos(i)), radius + ((radius / 2.00) * Math.Sin(i)));
                    textPointIsFound = true;
                }
            }
            v.Points.Add(new Point(radius, radius));
            fig.Tag = textPoint;
            fig.StrokeThickness = 2;
            fig.Stroke = Brushes.Black;
            bb.Add(v);

            myPathFigure.Segments = bb;


            PathGeometry myPathGeometry = new PathGeometry();
            myPathGeometry.Figures.Add(myPathFigure);
            fig.Data = myPathGeometry;
        }
        private static double DegreeToRadian(double deg)
        {
            return (Math.PI / 180) * deg;
        }
    }
}
